import Vue from 'vue'

const state = {
  current: {},    // 当前用户
}

const getters = {
  currentUser: state => state.current

}

const mutations = {
  SET_CURRENT_USER: (state, user) => {
    state.current = user
    localStorage.setItem('userId', user.id)
    localStorage.setItem('userCode', user.code)
    localStorage.setItem('officeId', user.officeId)
    if (user.token) {
      localStorage.setItem('appAuthorization', user.token)
    }
  }
}

const actions = {
  setCurrentUser: (context, user) => context.commit('SET_CURRENT_USER', user),
  logout: (context) => {
    global.$ajax.delete('/logout?deviceType=0').then(resp => {
      localStorage.removeItem('ms_username')
      localStorage.removeItem('autologin')
      localStorage.removeItem('appAuthorization')
      context.commit('SET_WORKBENCH_ROUTE', 'login')
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
